@extends('admin.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                @can('system.role.destroy')
                    <button class="layui-btn layui-btn-danger" id="listDelete" style="margin-right: 10px!important;">删 除</button>
                @endcan
                @can('system.role.store')
                    <a class="layui-btn layui-btn-normal " id="addrole">添 加</a>
                @endcan
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('system.role.update')
                        <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
                    @endcan
                    @can('system.role.assignPermission')
                        <a class="layui-btn layui-btn-sm layui-btn-warm" lay-event="permission"><i class="layui-icon">&#xe673;</i>权限</a>
                    @endcan
                </div>
            </script>
        </div>
    </div>




@endsection

@section('script')
    @can('system.role')
    <script>
        layui.use(['layer','table','form'],function () {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var load = layer.load();
            var where = {
                sorts:{}
            }
            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,height: 700
                ,url: "{{ route('system.role.data') }}" //数据接口
                ,where: where
                ,loading: false
                ,autoSort: false
                ,limits: [10, 15, 30]
                ,limit: 15
                ,page: {theme: '#1E9FFF'} //开启分页
                ,cols: [[ //表头
                    {checkbox: true,fixed: true}
                    ,{field: 'id', title: 'ID', width:80}
                    ,{field: 'name', title: '名称'}
                    ,{field: 'display_name', title: '显示名称'}
                    ,{field: 'created_at', title: '创建时间',sort:true}
                    ,{field: 'updated_at', title: '更新时间'}
                    ,{fixed: 'right', width: 260, align:'center', toolbar: '#options'}
                ]]
                ,done: function (res, curr, count) {
                    layer.close(load);
                }
            });

            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'del'){
                    layer.confirm('确认删除吗？', function(index){
                        $.post("{{ route('system.role.destroy') }}",{_method:'delete',ids:[data.id]},function (result) {
                            if (result.code==0){
                                obj.del(); //删除对应行（tr）的DOM结构
                            }
                            layer.close(index);
                            layer.msg(result.msg,{icon:6})
                        });
                    });
                } else if(layEvent === 'edit'){
                    layerIfram('/admin/role/'+data.id+'/edit','编辑角色',493,350);
                } else if (layEvent === 'permission'){
                    layer.full(layerIfram('/admin/role/'+data.id+'/permission','权限设置'));
                }
            });

            //监听排序
            table.on('sort(dataTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                where.sorts = {};
                where.sorts[obj.field] = obj.type;
                load = layer.load();
                dataTable.reload();
            });

            //按钮批量删除
            $("#listDelete").click(function () {
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length>0){
                    $.each(hasCheckData,function (index,element) {
                        ids.push(element.id)
                    })
                }
                if (ids.length>0){
                    layer.confirm('确认删除吗？', function(index){
                        $.post("{{ route('system.role.destroy') }}",{_method:'delete',ids:ids},function (result) {
                            if (result.code==0){
                                dataTable.reload()
                            }
                            layer.close(index);
                            layer.msg(result.msg,{icon:6})
                        });
                    })
                }else {
                    layer.msg('请选择删除项',{icon:5})
                }
            })


            //添加角色按钮
            $("#addrole").click(function () {
                layerIfram('{{ route('system.role.create') }}','添加角色',493,350);
            })

            //封装弹出层
            function layerIfram(url, title, w = 493, h = 580) {
                var iframlayer = layer.open({
                    type: 2,
                    title: title,
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: [w + 'px', h + 'px'],
                    content: url
                });
                return iframlayer;
            }
        })
    </script>
    @endcan
@endsection